var App = {};

App.Contact = {
	initialize : function(){
		this.baiduMap();
		this.contactForm();
	},
	baiduMap : function(){
		var map = new BMap.Map("allmap");            // 创建Map实例
        var point = new BMap.Point(108.91007,34.350581); // 创建点坐标
        map.centerAndZoom(point,14);                 // 初始化地图,设置中心点坐标和地图级别。
        map.enableScrollWheelZoom();
        var markeraa = new BMap.Marker(new BMap.Point(108.91007, 34.350581));  // 创建标注
        map.addOverlay(markeraa);              // 将标注添加到地图中
        map.addControl(new BMap.NavigationControl()); 
		map.addControl(new BMap.ScaleControl()); 
		map.addControl(new BMap.OverviewMapControl()); 
	},
	contactForm : function(){
		var form = $('#contactform');
		var $name = $('#name');
		var $telphone = $('#telphone');
		var $address = $('#address');
		var $message = $('#message');
		var $submit = $('#submit');
		var url = GLOBAL.SITEURL + '/email/contact';
		form.on('submit',function(){
			var vName = $name.val(),
				vTelphone = $telphone.val();
			if(vName == ''){
				alert('请填写您的名字');
				return false;
			}
			if(vTelphone == ''){
				alert('请填写您的联系电话');
				return false;
			}
			$submit.val('正在提交,请稍后...');
			$.ajax({
				url : url + '?'+ form.serialize(),
				dataType : 'json',
				success : function(data){
					if(data.success == 1){
						alert('您的资料已提交,请等待我们与您联系!');
					}else{
						alert('提交失败！');
					}
				},
				error : function(){

				},
				complete : function(){
					$submit.val('确定');
				}
			})
			return false;
		})
	}
}


// 店面展示
App.Store = {
	initialize : function(){
		this.ov = $('.overlay');
		this.map = $('.store-baidu-map');
		this.bdMap = null;
		this.stores = null;
		this.mapInit();
	},

	mapInit : function(){
		var self = this;
		var location = GLOBAL.LOCATION.split(',');
		this.bdMap = new BMap.Map("lp-map");            // 创建Map实例
        var point = new BMap.Point(location[0],location[1]); // 创建点坐标
        this.bdMap.centerAndZoom(point,15);                 // 初始化地图,设置中心点坐标和地图级别。
        this.bdMap.enableScrollWheelZoom();
        var markeraa = new BMap.Marker(new BMap.Point(location[0],location[1]));  // 创建标注
        this.bdMap.addOverlay(markeraa);              // 将标注添加到地图中
       	this._add_control();
        this.map.on('click', '.close-map', $.proxy(this._close_map, this));

        $('#stores').on('click','.store .sample-thumb,.store .consult-title', function(){
        	var store = $(this).closest('.store');
        	var name = store.find('.widget-title').html();
        	var point = store.data('coordinate');
        	self._choose_store(name,point.split(','));
        });
	},

	_close_map : function(){
		this.ov.fadeOut(200);
		this.map.css({'zIndex' : 0, visibility : 'hidden'});
	},
	_show_map : function(){
		this.ov.fadeIn(200);
		this.map.css({'zIndex' : 100, visibility : 'visible'});
	},
	_add_control : function(){
		this.bdMap.addControl(new BMap.NavigationControl()); 
		this.bdMap.addControl(new BMap.ScaleControl()); 
		this.bdMap.addControl(new BMap.OverviewMapControl()); 
	},
	_choose_store : function(name,location){
		this.bdMap.panTo(new BMap.Point(location[0],location[1]));
		var markeraa = new BMap.Marker(new BMap.Point(location[0],location[1]));  // 创建标注
        this.bdMap.addOverlay(markeraa);              // 将标注添加到地图中
		this.map.find('h2').html(name);
		this._show_map();
	}
}


// 加盟
App.Join = {

	initialize : function(){
		this.complete_address();
		this.baiduInit();
		this.submit();
	},
	complete_address : function(){
		var $address = $('#address');
		var $ov = $('.overlay');
		var $baidu = $('#SY-BaiduMap');
		var $close = $('#map_conf');
		var $sugges = $('#suggestId');
		$address.on('focus',function(){
			$ov.add($baidu).fadeIn(200);
		});
		$close.on('click',function(){
			$ov.add($baidu).fadeOut(200);
			$address.val($sugges.val());
		});
	},
	submit : function(){
		var form = $('#joinform');
		var submit = $('#submit');
		var url = GLOBAL.SITEURL + '/email/join';
		form.on('submit',function(){
			var name = $('#name');
			var phone = $('#phone');
			if(name.val() == ''){
				alert('请填写联系人');
				name.focus();
				return false;
			}
			if(phone.val() == ''){
				alert('请填写联系方式');
				phone.focus();
				return false;
			}
			submit.val('正在为您提交申请,请稍后...');
			$.ajax({
				url : url + '?'+ form.serialize(),
				dataType : 'json',
				success : function(data){
					if(data.success > 0){
						submit.val('提交申请');
						alert('您的申请已经提交,请等待审核');
					}else{
						submit.val('提交申请');
						alert(data.message);
					}
				}

			});
			return false;
		});
	},
	baiduInit : function(){
		    // 百度地图API功能
	    function G(id) {
	        return document.getElementById(id);
	    }
	    var oPointIpt = G("point");
	    var oPointSpan = document.getElementById('location').getElementsByTagName('span')[0];

	    var map = new BMap.Map("l-map");
	    map.centerAndZoom(new BMap.Point(108.954248,34.276726), 12);                   // 初始化地图,设置城市和地图级别。
	    map.enableScrollWheelZoom();

	    function myFuna(result){
	        var cityName = result.name;
	        map.setCenter(cityName);
	    }
	    var myCity = new BMap.LocalCity();
	    myCity.get(myFuna);


	    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
	        {"input" : "suggestId"
	        ,"location" : map
	    });

	    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
	    var str = "";
	        var _value = e.fromitem.value;
	        var value = "";
	        if (e.fromitem.index > -1) {
	            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        }    
	        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
	        
	        value = "";
	        if (e.toitem.index > -1) {
	            _value = e.toitem.value;
	            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        }    
	        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
	        G("searchResultPanel").innerHTML = str;
	    });

	    var myValue;
	    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	    var _value = e.item.value;
	        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
	        
	        setPlace();
	    });

	    function setPlace(){
	        map.clearOverlays();    //清除地图上所有覆盖物
	        function myFun(){
	            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
	            map.centerAndZoom(pp, 18);
	            map.addOverlay(new BMap.Marker(pp));    //添加标注
	            oPointIpt.value = pp.lat + "," + pp.lng;
				oPointSpan.innerHTML = pp.lat + "," + pp.lng;
	        }
	        var local = new BMap.LocalSearch(map, { //智能搜索
	          onSearchComplete: myFun
	        });
	        local.search(myValue);
	    }

	    map.addEventListener("click",function(e){
	        map.clearOverlays();
	        var markeraa = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));  // 创建标注
	        map.addOverlay(markeraa);              // 将标注添加到地图中
	        markeraa.setAnimation(BMAP_ANIMATION_BOUNCE);
	           //alert(e.point.lng + "," + e.point.lat);
	        oPointIpt.value =  e.point.lng + "," + e.point.lat;
	    	oPointSpan.innerHTML = e.point.lng + "," + e.point.lat;
	    });

	    G("suggestId").onkeydown = function(event){
	        if (event.keyCode == 13) {
	            myValue = this.value;
	            setPlace();
	        }
	    }
	    G("search_btn").onclick = function(){
	    	myValue = G("suggestId").value;
	    	setPlace();
	    }
	}
};



App.Product = {

	initialize : function(){
		
	},

	ligthBox : function(){
		
	}

};